package com.app.ui.view.list.head;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.app.ui.view.list.BaseLisetView;
import com.app.utiles.other.DLog;


public class ListRefreshCustom extends BaseLisetView {
    //最后的y指标
    private float lastY = -1;
    private final static float OFFSET_RADIO = 1.8f;
    private BaseRefreshLayout baseRefreshLayout;
    private OnRefreshListener onRefreshListener;
    //true :禁止使用下拉刷新
    private boolean isRefreshEnabled;

    public ListRefreshCustom(Context context) {
        super(context);
        initWithContext(context);
    }

    public ListRefreshCustom(Context context, AttributeSet attrs) {
        super(context, attrs);
        initWithContext(context);
    }

    public ListRefreshCustom(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initWithContext(context);
    }

    private void initWithContext(Context context) {

    }


    public void setHeadType(int type) {
        switch (type) {
            case 1:
                //下拉刷新
                baseRefreshLayout = new HeaderViewRefresh(getContext());
                break;
            case 2:
                //会话加载历史消息
                baseRefreshLayout = new HeaderViewRefreshChat(getContext());
                break;
        }
        onRefreshListener = baseRefreshLayout.getRenovationListener();
        addHeaderView(baseRefreshLayout);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                DLog.e("ACTION_DOWN", "================");
                lastY = ev.getRawY();
                isHeadShow = false;
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    private boolean isHeadShow;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (isRefreshEnabled) {
            return super.onTouchEvent(ev);
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float deltaY = ev.getRawY() - lastY;
                if (onRefreshListener == null) {
                    break;
                }
                if (getFirstVisiblePosition() == 0 && (baseRefreshLayout.getVisiableHeight() > 0 || deltaY > 0)
                        && !baseRefreshLayout.isRefresh()) {
                    onRefreshListener.onTouchEvent(deltaY / OFFSET_RADIO);
                    isHeadShow = true;
                } else {
                    lastY = ev.getRawY();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (onRefreshListener == null) {
                    break;
                }
                boolean isR = onRefreshListener.onTouchEventStop();
                if (isR && onLoadingListener != null) {
                    onLoadingListener.onLoading(true);
                }
                break;
        }
        return isHeadShow ? false : super.onTouchEvent(ev);
    }


    // 刷新完成
    public void onRefreshComplete() {
        if (onRefreshListener == null) {
            return;
        }
        onRefreshListener.setRefreshComplete();

    }

    // 触发刷新和加载更多接口.

    private OnLoadingListener onLoadingListener;

    public void setOnLoadingListener(OnLoadingListener onLoadingListener) {
        this.onLoadingListener = onLoadingListener;
    }


    public void setRefreshEnabled(boolean isRefreshEnabled) {
        this.isRefreshEnabled = isRefreshEnabled;
    }

    //刷新监听
    public interface OnLoadingListener {
        // isRefresh true 刷新 false 加载更多
        void onLoading(boolean isRefresh);
    }


    //头部刷新的监听
    public interface OnRefreshListener {
        //手指移动
        void onTouchEvent(float f);

        //判断是否可以刷新
        boolean onTouchEventStop();

        //刷新完成
        void setRefreshComplete();
    }


}
